home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / libiberty / argv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  7.0 KB  |  361 lines

  1. /* Create and destroy argument vectors (argv's)
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.    Written by Fred Fish @ Cygnus Support
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2 of the License, or
  8.    (at your option) any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20. /*  Create and destroy argument vectors.  An argument vector is simply an
  21.     array of string pointers, terminated by a NULL pointer. */
  22.  
  23. /* AIX requires this to be the first thing in the file. */
  24. #ifdef __GNUC__
  25. #define alloca __builtin_alloca
  26. #else /* not __GNUC__ */
  27. #ifdef sparc
  28. #include <alloca.h>
  29. extern char *__builtin_alloca();  /* Stupid include file doesn't declare it */
  30. #else
  31. #ifdef _AIX
  32.  #pragma alloca
  33. #else
  34. char *alloca ();
  35. #endif
  36. #endif /* sparc */
  37. #endif /* not __GNUC__ */
  38.  
  39. #define isspace(ch) ((ch) == ' ' || (ch) == '\t')
  40.  
  41. #include "alloca-conf.h"
  42.  
  43. /*  Routines imported from standard C runtime libraries. */
  44.  
  45. #ifdef __STDC__
  46.  
  47. #include <stddef.h>
  48. extern void *memcpy (void *s1, const void *s2, size_t n);    /* 4.11.2.1 */
  49. extern size_t strlen (const char *s);                /* 4.11.6.3 */
  50. extern void *malloc (size_t size);                /* 4.10.3.3 */
  51. extern void free (void *ptr);                    /* 4.10.3.2 */
  52. extern char *strdup (const char *s);                /* Non-ANSI */
  53.  
  54. #else    /* !__STDC__ */
  55.  
  56. extern char *memcpy ();        /* Copy memory region */
  57. extern int strlen ();        /* Count length of string */
  58. extern char *malloc ();        /* Standard memory allocater */
  59. extern void free ();        /* Free malloc'd memory */
  60. extern char *strdup ();        /* Duplicate a string */
  61.  
  62. #endif    /* __STDC__ */
  63.  
  64. #ifndef NULL
  65. #define NULL 0
  66. #endif
  67.  
  68. #ifndef EOS
  69. #define EOS '\000'
  70. #endif
  71.  
  72. /*  Local data. */
  73.  
  74. static int argc = 0;
  75. static int maxargc = 0;
  76. static char **argv = NULL;
  77.  
  78. /*
  79.  
  80. NAME
  81.  
  82.     expandargv -- initialize or expand an argv
  83.  
  84. SYNOPSIS
  85.  
  86.     static int expandargv ()
  87.  
  88. DESCRIPTION
  89.  
  90.     Expand argv by a factor of two (or initialize argv for the first
  91.     time if there is no current argv).
  92.  
  93. RETURNS
  94.  
  95.     Returns the new max value for argc, or zero if any error.
  96. */
  97.  
  98. static int expandargv ()
  99. {
  100.   register int argvsize;
  101.   register char **newargv;
  102.  
  103.   /* Increase maxargc */
  104.   if (maxargc == 0)
  105.     {
  106.       maxargc = 16;
  107.     }
  108.   else
  109.     {
  110.       maxargc *= 2;
  111.     }
  112.   argvsize = (maxargc + 1) * sizeof (char *);
  113.   if ((newargv = (char **) malloc (argvsize)) == NULL)
  114.     {
  115.       maxargc = 0;
  116.     }
  117.   else
  118.     {
  119.       if (argv != NULL)
  120.     {
  121.       /* Copy and free old args */
  122.       memcpy (newargv, argv, argc * sizeof (char *));
  123.       free (argv);
  124.     }
  125.       argv = newargv;
  126.     }
  127.   return (maxargc);
  128. }
  129.  
  130.  
  131. /*
  132.  
  133. NAME
  134.  
  135.     freeargv -- free an argument vector
  136.  
  137. SYNOPSIS
  138.  
  139.     void freeargv (vector)
  140.     char **vector;
  141.  
  142. DESCRIPTION
  143.  
  144.     Free an argument vector that was built using buildargv.  Simply scans
  145.     through the vector, freeing the memory for each argument, and then
  146.     finally the vector itself.
  147.  
  148. RETURNS
  149.  
  150.     No value.
  151.  
  152. */
  153.  
  154. void freeargv (vector)
  155. char **vector;
  156. {
  157.   register char **scan;
  158.  
  159.   if (vector != NULL)
  160.     {
  161.       for (scan = vector; *scan != NULL; scan++)
  162.     {
  163.       free (*scan);
  164.     }
  165.       free (vector);
  166.     }
  167. }
  168.  
  169. /*
  170.  
  171. NAME
  172.  
  173.     buildargv -- build an argument vector from a string
  174.  
  175. SYNOPSIS
  176.  
  177.     char **buildargv (sp)
  178.     char *sp;
  179.  
  180. DESCRIPTION
  181.  
  182.     Given a pointer to a string, parse the string extracting fields
  183.     separated by whitespace and optionally enclosed within either single
  184.     or double quotes (which are stripped off), and build a vector of
  185.     pointers to copies of the string for each field.  The input string
  186.     remains unchanged.
  187.  
  188.     All of the memory for the pointer array and copies of the string
  189.     is obtained from malloc.  All of the memory can be returned to the
  190.     system with the single function call freeargv, which takes the
  191.     returned result of buildargv, as it's argument.
  192.  
  193.     The memory for the argv array is dynamically expanded as necessary.
  194.  
  195. RETURNS
  196.  
  197.     Returns a pointer to the argument vector if successful. Returns NULL
  198.     if the input string pointer is NULL or if there is insufficient
  199.     memory to complete building the argument vector.
  200.  
  201. NOTES
  202.  
  203.     In order to provide a working buffer for extracting arguments into,
  204.     with appropriate stripping of quotes and translation of backslash
  205.     sequences, we allocate a working buffer at least as long as the input
  206.     string.  This ensures that we always have enough space in which to
  207.     work, since the extracted arg is never larger than the input string.
  208.  
  209. */
  210.  
  211. char **buildargv (input)
  212. char *input;
  213. {
  214.   register char *arg;
  215.   register int squote = 0;
  216.   register int dquote = 0;
  217.   register int bsquote = 0;
  218.   char *copybuf;
  219.  
  220.   argv = NULL;
  221.   if (input != NULL)
  222.     {
  223.       argc = 0;
  224.       maxargc = 0;
  225.       copybuf = alloca (strlen (input) + 1);
  226.       while (*input != EOS)
  227.     {
  228.       /* Pick off argv[argc] */
  229.       while (isspace (*input))
  230.         {
  231.           input++;
  232.         }
  233.       /* Check to see if argv needs expansion. */
  234.       if (argc >= maxargc)
  235.         {
  236.           if (expandargv () == 0)
  237.         {
  238.           freeargv (argv);
  239.           argv = NULL;
  240.           break;
  241.         }
  242.         }
  243.       /* Begin scanning arg */
  244.       arg = copybuf;
  245.       while (*input != EOS)
  246.         {
  247.           if (isspace (*input) && !squote && !dquote && !bsquote)
  248.         {
  249.           break;
  250.         }
  251.           else
  252.         {
  253.           if (bsquote)
  254.             {
  255.               bsquote = 0;
  256.               *arg++ = *input;
  257.             }
  258.           else if (*input == '\\')
  259.             {
  260.               bsquote = 1;
  261.             }
  262.           else if (squote)
  263.             {
  264.               if (*input == '\'')
  265.             {
  266.               squote = 0;
  267.             }
  268.               else
  269.             {
  270.               *arg++ = *input;
  271.             }
  272.             }
  273.           else if (dquote)
  274.             {
  275.               if (*input == '"')
  276.             {
  277.               dquote = 0;
  278.             }
  279.               else
  280.             {
  281.               *arg++ = *input;
  282.             }
  283.             }
  284.           else
  285.             {
  286.               if (*input == '\'')
  287.             {
  288.               squote = 1;
  289.             }
  290.               else if (*input == '"')
  291.             {
  292.               dquote = 1;
  293.             }
  294.               else
  295.             {
  296.               *arg++ = *input;
  297.             }
  298.             }
  299.           input++;
  300.         }
  301.         }
  302.       *arg = EOS;
  303.       /* Add arg to argv */
  304.       if ((argv[argc++] = strdup (copybuf)) == NULL)
  305.         {
  306.           freeargv (argv);
  307.           argv = NULL;
  308.           break;
  309.         }
  310.     }
  311.       if (argv != NULL)
  312.     {
  313.       argv[argc] = NULL;
  314.     }
  315.     }
  316.   return (argv);
  317. }
  318.  
  319. #ifdef MAIN
  320.  
  321. /* Simple little test driver. */
  322.  
  323. static char *tests[] =
  324. {
  325.   "a simple command line",
  326.   "arg 'foo' is single quoted",
  327.   "arg \"bar\" is double quoted",
  328.   "arg \"foo bar\" has embedded whitespace",
  329.   "arg 'Jack said \\'hi\\'' has single quotes",
  330.   "arg 'Jack said \\\"hi\\\"' has double quotes",
  331.   NULL
  332. };
  333.  
  334. main ()
  335. {
  336.   char **argv;
  337.   char **test;
  338.   char **targs;
  339.  
  340.   for (test = tests; *test != NULL; test++)
  341.     {
  342.       printf ("buildargv(\"%s\")\n", *test);
  343.       if ((argv = buildargv (*test)) == NULL)
  344.     {
  345.       printf ("failed!\n\n");
  346.     }
  347.       else
  348.     {
  349.       for (targs = argv; *targs != NULL; targs++)
  350.         {
  351.           printf ("\t\"%s\"\n", *targs);
  352.         }
  353.       printf ("\n");
  354.     }
  355.       freeargv (argv);
  356.     }
  357.  
  358. }
  359.  
  360. #endif    /* MAIN */
  361.